package no.progark.golf;

public class Player {
	private int ID;
	private String name;
	private Ball ball;
	private int totalScore = 0;
	private int shots = 0;
	private final int MAXNAMELENGTH = 12;
	
	public Player(){
		
	}
	
	public Player(String name){
		this.name = (validateName(name))?name:validVersionOfName(name);
	}
	
	/**
	 * creates a valid version of name entered if that name is not valid.
	 * @param name the name entered by user
	 * @return a valid version of name used by game for displaying player
	 */
	private String validVersionOfName(String name) {
		name.trim();
		
		if(name.length() > MAXNAMELENGTH)name = name.substring(0, MAXNAMELENGTH);
		name.trim();
		return (name.length() == 0)?new String("default"):name;
	}

	
	
	/**
	 * Checks if name is valid (length primarily). 
	 * @return true if valid, false if not.
	 */
	private boolean validateName(String name) {
		name.trim();
		if(name.length() > MAXNAMELENGTH || name.length() < 1)return false;
		return true;
	}

	/**
	 * increment shots player has used on current map
	 */
	public void updateShots() {
		shots++;
	}
	
	
	/**
	 * reset shots when changed map 
	 */
	public void resetShots(){
		shots = 0;
	}
	
	
	/**
	 * updates score of player
	 * @param points amount of points to add to players total
	 */
	public void updateScore(int points){
		totalScore += points;
	}


	public Ball getBall() {
		return ball;
	}


	public void setBall(Ball ball) {
		this.ball = ball;
	}


	public int getTotalScore() {
		return totalScore;
	}


	public void setTotalScore(int totalScore) {
		this.totalScore = totalScore;
	}


	public int getShots() {
		return shots;
	}


	public void setShots(int shots) {
		this.shots = shots;
	}

	/**
	 * checks if player is done with map, e.g. has reached goal. 
	 * NOT DONE!
	 * @return temporarily always returns false
	 */
	public boolean isDone() {
		return getBall().getDone();
	}

	protected void setId(int playerId) {
		ID = playerId;
	}

	public CharSequence getName() {
		return name;
	}
	
	public String toString(){
		return name;
	}

}
